<template>
  <div>
    <!--修改商品分类对话框 -->
    <el-dialog title="修改商品分类" v-model="dialogVisible" :close-on-click-modal="false" @close="resetForm">
      <el-form :model="form" :rules="rules" ref="modifyForm">
        <el-form-item label="分类名称" prop="name" label-width="80px">
          <el-input v-model="form.name" placeholder="请商品分类名称"></el-input>
        </el-form-item>
        <el-form-item label="分类级别" prop="level" label-width="80px">
          <el-radio-group v-model="form.level" disabled title="商品分类级别不可以修改">
            <el-radio-button label="一级" :value="1"/>
            <el-radio-button label="二级" :value="2"/>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="分类描述" prop="desc" label-width="80px">
          <el-input
              type="textarea"
              v-model="form.desc"
              placeholder="请输入分类描述"
              :rows="4"
              resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status" label-width="80px">
          <el-radio-group v-model="form.delStatus">
            <el-radio-button label="正常" :value="1"/>
            <el-radio-button label="停用" :value="9"/>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span class="dialog-footer">
				<el-button @click="resetForm">重填</el-button>
				<el-button type="primary" @click="submitForm">修改</el-button>
			</span>
    </el-dialog>
  </div>
</template>
<script setup>
import {ElMessage} from 'element-plus';
import {reactive, ref, toRefs, watch} from 'vue';
import apiUtils from '@/assets/js/api-utils.js';

const data = reactive({
  dialogVisible: false, // 控制对话框的显示状态
  //表单字段
  form: {
    name: '',
    desc: '',
    delStatus: '1',
  },
  //表单验证规则
  rules: {
    name: [{required: true, message: '请输入分类名称', trigger: 'change'}],
    desc: [{required: true, message: '请输入分类描述', trigger: 'change'}],
    delStatus: [{required: true, message: '请选择分类状态', trigger: 'change'}],
  },
});
//创建学生表单组件实例
let modifyForm = ref(null);
let {
  dialogVisible,
  form,
  rules
} = toRefs(data);
//提交form表单
const submitForm = () => {
  modifyForm.value.validate((valid) => {
    if (valid) {
      modifyCategory();
    } else {
      ElMessage({message: '提交失败!', type: 'error'});
      return false;
    }
  });
};
const  modifyCategory = async () => {
  let params = {
    id: form.value.id,
    name: form.value.name,
    desc: form.value.desc,
    delStatus: form.value.delStatus,
  }
  let id = form.value.id;
  let response = await apiUtils.category.modify(params);
  if (200 !== response.code) {
    ElMessage.error(response.message);
    return;
  }
  let success = response.data;
  if (success) {
    ElMessage.success('修改成功');
    dialogVisible.value = false;
  } else {
    ElMessage.warning('修改失败,记录不存在');
  }
}

//重置表单
const resetForm = () => {
  modifyForm.value.resetFields();
};
//暴露dialogVisible,父组件可以直接使用
defineExpose({
  dialogVisible,
  form
});
</script>


<style scoped>
/* 样式可以根据需要自定义 */
</style>